package com.study.talk.module.netty.handler;

import com.study.talk.module.entity.GroupInfo;
import com.study.talk.module.entity.GroupUserInfo;
import com.study.talk.module.entity.UserInfo;
import com.study.talk.module.netty.message.Attributes;
import com.study.talk.module.netty.message.Session;
import com.study.talk.module.netty.message.request.GroupMessageRequest;
import com.study.talk.module.netty.message.response.GroupMessageResponse;
import com.study.talk.module.netty.message.response.GroupMessageSelfResponse;
import com.study.talk.module.netty.message.response.MessageSelfResponse;
import com.study.talk.module.service.GroupInfoService;
import com.study.talk.module.service.GroupUserInfoService;
import com.study.talk.module.service.UserInfoService;
import com.study.talk.module.service.UserService;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * 群消息处理
 */

@Component
@Slf4j
@ChannelHandler.Sharable
public class GroupMessageHandler extends SimpleChannelInboundHandler<GroupMessageRequest> {

    @Resource
    private UserService userService;

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private GroupUserInfoService groupUserInfoService;

    @Resource
    private GroupInfoService groupInfoService;

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupMessageRequest msg) throws Exception {
        log.info("群消息发出事件,{}", msg);

        GroupInfo groupInfo = groupInfoService.getById(msg.getGroupId());

        Session session = ctx.channel().attr(Attributes.SESSION).get();

        if (groupInfo == null) { //群聊有可能已经被解散了
            GroupMessageSelfResponse groupMessageSelfResponse = new GroupMessageSelfResponse();
            groupMessageSelfResponse.setSuccess(false);
            groupMessageSelfResponse.setGroupId(msg.getGroupId());
            groupMessageSelfResponse.setReason("无法在已经解散的群聊中发送消息");
            groupMessageSelfResponse.setDateTime(msg.getDateTime());
            userService.sendMessage(session.getUserId(),groupMessageSelfResponse);
            return;
        }

        if (!groupUserInfoService.queryHasMember(groupInfo.getId(), session.getUserId())) {
            GroupMessageSelfResponse groupMessageSelfResponse = new GroupMessageSelfResponse();
            groupMessageSelfResponse.setSuccess(false);
            groupMessageSelfResponse.setGroupId(groupInfo.getId());
            groupMessageSelfResponse.setReason("你不是该群成员！");
            groupMessageSelfResponse.setDateTime(msg.getDateTime());
            userService.sendMessage(session.getUserId(),groupMessageSelfResponse);
            return;
        }

        GroupMessageSelfResponse groupMessageSelfResponse = new GroupMessageSelfResponse();
        groupMessageSelfResponse.setSuccess(true);
        groupMessageSelfResponse.setGroupId(msg.getGroupId());
        groupMessageSelfResponse.setDateTime(msg.getDateTime());
        groupMessageSelfResponse.setMessageType(msg.getMessageType());
        groupMessageSelfResponse.setContent(msg.getContent());
        userService.sendMessage(session.getUserId(), groupMessageSelfResponse);


        List<UserInfo> members = groupInfoService.getMembersUserInfo(msg.getGroupId());
        UserInfo userInfo = userInfoService.getById(session.getUserId());
        GroupMessageResponse groupMessageResponse = new GroupMessageResponse();
        groupMessageResponse.setMessageType(msg.getMessageType());
        groupMessageResponse.setGroupId(groupInfo.getId());
        groupMessageResponse.setAvatar(userInfo.getAvatar());
        groupMessageResponse.setSendUserName(userInfo.getUsername());
        groupMessageResponse.setSendUserNickName(userInfo.getNickName());
        groupMessageResponse.setSendUserId(userInfo.getId());
        groupMessageResponse.setContent(msg.getContent());
        groupMessageResponse.setDateTime(msg.getDateTime());

        for (UserInfo member : members) {
            if (member.getId().equals(session.getUserId())) continue;
            userService.sendMessage(member.getId(), groupMessageResponse);
        }

    }
}
